package com.steak.meta;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 刘牌
 * @Title:
 * @date 2022-05-2416:21
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //URL httpUrl = new URL("https://www.fisco.com.cn/upload/files/20210706/1625552021860086.pdf");
        File file = new File("C:\\Users\\DELL\\Desktop\\blockchain.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = IOUtils.toByteArray(fileInputStream, fileInputStream.available());
        //byte[] bytes = IOUtils.toByteArray(httpUrl);
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(bytes);
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.out.println(encodeStr);
//        List<Integer> linkedList = new LinkedList<>();
//        List<Integer> arrayList = new ArrayList<>();
//        long start = System.currentTimeMillis();
//        for (int i = 0 ; i < 50000000 ; i++){
//            arrayList.add(i);
//        }
//        long cu = System.currentTimeMillis() - start;
//        System.out.println(arrayList.size());
//        System.out.println(cu);
    }

    private static String byte2Hex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length()==1){
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}
